package cn.sh.library.gmwx.controller.hsly;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sh.library.gmwx.bean.ArchitectureSearchBean;
import cn.sh.library.gmwx.controller.BaseController;
import cn.sh.library.gmwx.dto.Pager;
import cn.sh.library.gmwx.service.hsly.ArchitectureService;
import cn.sh.library.gmwx.utils.DateUtilC;
import cn.sh.library.gmwx.utils.StringUtilC;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/architecture")
public class ArchitectureController extends BaseController {
	
	@Resource
	private ArchitectureService architectureService;
	
	
	/**
	 * 获取红色旅游建筑列表
	 * 
	 * @param freetext
	 * @return Map<String,Object>
	 */
	@ResponseBody
	@RequestMapping(value = "/getArchitecture", method = RequestMethod.GET)
	public Map<String, Object> getArchitecture(HttpServletRequest request,
			ArchitectureSearchBean search, Pager pager) throws Exception {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> architectureList = new ArrayList<Map<String, Object>>();
		architectureList = architectureService.getArchitecture(search, pager);
		convertPlaceList(architectureList);
		result.put("data", architectureList);
		result.put("pager", pager);
		return result;
	}
	
	/**
	 * 获取红色旅游建筑详情
	 * 
	 * @param uri
	 * @return Map<String,Object>
	 */
	@ResponseBody
	@RequestMapping(value = "/getArchitectureDetail", method = RequestMethod.GET)
	public Map<String, Object> getArchitectureDetail(HttpServletRequest request,
			String uri) throws Exception {
		Map<String, Object> architectureDetail = architectureService
				.architecture_detail(uri);
		Map<String,Object> architecture=(Map<String, Object>) architectureDetail.get("data");
		ConvertPlaceMap(architecture);
		 return architectureDetail;
	}
	
	/**
	 * 获取List
	 * 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getRdf", method = RequestMethod.GET)
	public Map<String, Object> getRdf(HttpServletRequest request,String graph,String uri) throws Exception {
		Map<String, Object> result = new HashMap<>();
		result.put("rdfs", architectureService.getRDF(graph, uri));
		return result;
	}
	
	
	
	private void convertPlaceList(List<Map<String, Object>> architectureList)
			throws ClientProtocolException, IOException {

		/* new add by fjj begin 2018-10-11 */
		Date d1 = DateUtilC.getNowDateTime();
		System.out.println("----执行开始：" + DateUtilC.getNowDateTime());
		for (Map<String, Object> map : architectureList) {
			ConvertPlaceMap(map);
		}
		/* new add by fjj begin 2018-10-25 */
		Date d2 = DateUtilC.getNowDateTime();
		long interval = (d2.getTime() - d1.getTime()) / 1000;
		System.out.println("执行用时：" + interval + "秒");
	}
	
	private void ConvertPlaceMap(Map<String, Object> map)
			throws ClientProtocolException, IOException {
		String  placeUri = StringUtilC.getString(map.get("placeUri"));
		Map<String, Object> _mapTemp = new HashMap<String, Object>();
		getPlaceDetail(placeUri, map);

	}
	
	private void getPlaceDetail(String placeUri,
			Map<String, Object> _mapTemp) throws ClientProtocolException,
			IOException {
		
		List<Map<String, Object>> labelList = new ArrayList<Map<String, Object>>();
		
			
				if (!StringUtilC.isEmpty(placeUri)) {
					String strURl = String.format(placeDetailApi, placeUri);
					// 地点网站接口模式，访问接口得到jsonld数据
					Object jsonApiResult = StringUtilC.invokeWebApi(strURl);
					if (!StringUtilC.isEmpty(jsonApiResult)) {
						// 得到地点接口获取到的人物详细信息Map
						Map<String, Object> _map = new HashMap<String, Object>();
						_map = (Map<String, Object>) (JSONObject
								.fromObject(jsonApiResult));
						labelList = (List<Map<String, Object>>) _map.get("label");
						
						//循环得到value
						Map<String, Object> _placeMap = new HashMap<String, Object>();
						
						for(Map<String, Object> map:labelList) {
							
							if("chs".equals(map.get("@language"))) {
								
								_mapTemp.put("placeValue", map.get("@value"));
								break;
							}
							
						}
						
//						_mapTemp.add(_placeMap);
					}
				}

			
		
	}

}
